package cn.sc.allinpay.allfavour.business.main;

import android.os.Handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.j256.ormlite.stmt.DeleteBuilder;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.pai.mvp.callback.MpCallBack;
import cn.sc.allinpay.allfavour.base.connect.http.AppRequest;
import cn.sc.allinpay.allfavour.base.connect.http.RequestManager;
import cn.sc.allinpay.allfavour.base.db.DbDao;
import cn.sc.allinpay.allfavour.base.model.BaseModel;
import cn.sc.allinpay.allfavour.business.common.data.CheckCouponsRecord;
import cn.sc.allinpay.allfavour.business.common.data.DistCouponRecord;
import cn.sc.allinpay.allfavour.business.common.data.SaleCouponRecord;
import cn.sc.allinpay.allfavour.business.common.data.SpOrderInfo;

/**
 * 主界面
 * author：pany
 * on 2017/12/5 14:44
 */
public class MainModel extends BaseModel {


    /**
     * 删除7天前的验券，发券和售券流水
     */
    public void deleteCheckRecord(final MpCallBack<Integer, String> mpCallBack) {
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                Date date = new Date();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_MONTH, -7);
                date = calendar.getTime();
                String dates = sdf.format(date);
                try {
                    final int checkResult = delCheckRecord(dates);//删除验券
                    final int saleResult = delSaleRecord(dates);//删除售券
                    final int delDistRecord = delDistRecord(dates);//删除发券
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            mpCallBack.onSuccess((checkResult + saleResult + delDistRecord));
                        }
                    });
                } catch (SQLException e) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            mpCallBack.onFailure("", "流水清除失败");
                        }
                    });
                }

            }
        }).start();

    }

    /**
     * 删除验券流水
     *
     * @param dates
     * @return
     * @throws SQLException
     */
    private int delCheckRecord(String dates) throws SQLException {
        DbDao<CheckCouponsRecord> dbDao = new DbDao<>(CheckCouponsRecord.class);
        DeleteBuilder<CheckCouponsRecord, Integer> deleteBuilder = dbDao.deleteBuilder();
        deleteBuilder.where().le("TRANSDATE", dates);
        return deleteBuilder.delete();
    }

    /**
     * 删除售券流水
     *
     * @param dates
     * @return
     * @throws SQLException
     */
    private int delSaleRecord(String dates) throws SQLException {
        DbDao<SaleCouponRecord> dbDao = new DbDao<>(SaleCouponRecord.class);
        DeleteBuilder<SaleCouponRecord, Integer> deleteBuilder = dbDao.deleteBuilder();
        deleteBuilder.where().le("TRANSDATE", dates);
        return deleteBuilder.delete();
    }


    /**
     * 删除发券流水
     *
     * @param dates
     * @return
     * @throws SQLException
     */
    private int delDistRecord(String dates) throws SQLException {
        DbDao<DistCouponRecord> dbDao = new DbDao<>(DistCouponRecord.class);
        DeleteBuilder<DistCouponRecord, Integer> deleteBuilder = dbDao.deleteBuilder();
        deleteBuilder.where().le("TRANSDATE", dates);
        return deleteBuilder.delete();
    }

    public void payNotify(final String order_id, final String trans_way, final String traxstatus, final String trans_time, final String c_amount, final String a_amount, final String transactionNumber, final MpCallBack<JSONObject, String> mpCallBack) {
        RequestManager.payNotify(order_id, trans_way, traxstatus, trans_time, c_amount, a_amount, transactionNumber, new AppRequest.RequestCall() {
            @Override
            protected void onSuccess(JSONObject response) {
                mpCallBack.onSuccess(response);
            }

            @Override
            public void onFailure(JSONObject response, String message) {
                mpCallBack.onFailure(response != null ? response.toString() : null, message);
            }
        });
    }

    public void spServer(final MpCallBack<String, String> mpCallBack) {
        RequestManager.spServer(new AppRequest.PosRequestCall() {
            @Override
            public void onFailure(JSONObject response, String message) {

            }

            @Override
            protected void onSuccess(JSONObject response) {
                String serverName = response.getString("data");
                mpCallBack.onSuccess(serverName);
            }
        });
    }

}
